package com.example.springboot2.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot2.base.web.BaseController;
import com.example.springboot2.oa.entity.TCar;
import com.example.springboot2.oa.entity.TUser;
import com.example.springboot2.oa.service.ITCarService;
import com.example.springboot2.tools.MakeResponse;
import com.example.springboot2.tools.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;

@CrossOrigin
@RestController
@RequestMapping("/oa/t-car")
public class TCarController extends BaseController {
    @Autowired
    private ITCarService carService;

    /**
     * 用户列表
     * @param request
     * @return
     */
    @PostMapping("listAll")
    public ResponseMessage listAll(HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        QueryWrapper<TCar> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("create_time");
        List<TCar> data = carService.list(wrapper);
        return MakeResponse.success("200", "成功", data);
    }

    /**
     * 添加车辆
     * @param car
     * @param request
     * @return
     */
    @PostMapping("add")
    public ResponseMessage add(@RequestBody TCar car, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        car.setCreateTime(new Date());

        if (!carService.save(car)) {
            return MakeResponse.success("500", "新增失败");
        }

        return MakeResponse.success("200", "新增成功");
    }

    /**
     * 批量删除用户
     * @param carList
     * @param request
     * @return
     */
    @PostMapping("delete")
    public ResponseMessage delete(@RequestBody List<TCar> carList, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        List<String> carIdList = new ArrayList<>();
        carList.forEach(new Consumer<TCar>() {
            @Override
            public void accept(TCar car) {
                carIdList.add(car.getCarId());
            }
        });

        QueryWrapper<TCar> wrapper = new QueryWrapper<>();
        wrapper.in("car_id", carIdList);
        if (!carService.remove(wrapper)) {
            return MakeResponse.success("500", "批量删除失败");
        }

        return MakeResponse.success("200", "批量删除成功");
    }

    /**
     * 更新车辆信息
     * @param car
     * @param request
     * @return
     */
    @PostMapping("modify")
    public ResponseMessage modify(@RequestBody TCar car, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }


        car.setUpdateTime(new Date());
        QueryWrapper<TCar> wrapper = new QueryWrapper<>();
        wrapper.eq("car_id", car.getCarId());
        if (!carService.update(car, wrapper)) {
            return MakeResponse.success("500", "修改失败");
        }

        return MakeResponse.success("200", "修改成功");
    }
}
